<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
<head>
  <meta charset="utf-8">
  <title>用户列表</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" th:href="@{/static/layuiadmin/layui/css/layui.css}" media="all">
  <link rel="stylesheet" th:href="@{/static/layuiadmin/style/admin.css}" media="all">
</head>
<body>
  <div class="layui-fluid">
    <div class="layui-card">
      <div class="layui-form layui-card-header layuiadmin-card-header-auto">
        <div class="layui-form-item">
          <div class="layui-inline">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-block">
              <input type="text" name="userName" placeholder="请输入用户名" class="layui-input">
            </div>
          </div>
          <div class="layui-inline">
            <label class="layui-form-label">邮箱</label>
            <div class="layui-input-block">
              <input type="text" name="email" placeholder="请输入邮箱" class="layui-input">
            </div>
          </div>
          <div class="layui-inline">
            <label class="layui-form-label">手机号</label>
            <div class="layui-input-block">
              <input type="text" name="phonenumber" placeholder="请输入手机号" class="layui-input">
            </div>
          </div>
          <div class="layui-inline">
            <button class="layui-btn layuiadmin-btn-useradmin" lay-submit lay-filter="search">
              <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
            </button>
            <button sec:authorize="hasAuthority('user:add')" class="layui-btn layuiadmin-btn-useradmin" data-type="addView">添加</button>
          </div>
        </div>
      </div>
      <div class="layui-card-body">
        <table id="userTable" lay-filter="userTable"></table>
        <script type="text/html" id="enabledTpl">
          <input type="checkbox" lay-filter="enabled" value="{{d.id}}" lay-skin="switch" lay-text="激活|冻结" {{d.enabled==='Y'?'checked':''}} />
        </script>
        <script type="text/html" id="tableBar">
          <a sec:authorize="hasAuthority('user:edit')" class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
          <a sec:authorize="hasAuthority('user:del')" class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
        </script>
      </div>
    </div>
  </div>
  <script th:src="@{/static/layuiadmin/layui/layui.js}"></script>
  <script th:inline="javascript">
  let ctx = [[${#request.getContextPath()}]];
  layui.config({
      base: '/static/layuiadmin/' //静态资源所在路径
  }).extend({
    index: 'lib/index', //主入口模块
  }).use(['index', 'useradmin', 'table','crud'], function() {
      let $ = layui.$
          ,form = layui.form
          ,crud = layui.crud
          , table = layui.table;
      //监听搜索
      form.on('submit(search)', function(data){
          let field = data.field;
          //执行重载
          table.reload('userTable', {
              where: field
          });
      });
      // 修改user状态
      form.on('switch(enabled)', function (obj) {
          let userId = obj.elem.value;
          let checked = obj.elem.checked ? 'Y' : 'N';
          active.changeUserEnabled(userId,checked);
      });
      // 查询列表接口
      table.render({
          elem: '#userTable'
          ,height: 'full-110'
          ,url:  ctx + '/sysUser/list'
          ,page: true //开启分页,
          ,limit: 10 // 多少条
          ,request: {
            pageName: 'pageNum', //页码的参数名称，默认：page
            limitName: 'pageSize' //每页数据量的参数名，默认：limit
          }
          ,parseData: function (result) {
              return {
                   "code": result.code,
                   "count": result.data.total,
                   "data": result.data.records
              }
          }
          ,response: {
            statusCode: 200 //成功的状态码，默认：0
          }
          ,cols: [[ //表头
              {field: 'id', title: 'ID', sort: true, fixed: 'left'}
              ,{field: 'avatar', title: "头像",
                  templet: function (d) {
                    if (d.avatar) {
                      return '<div><img alt="" width="50%" height="50%" src='+d.avatar+'></div>'
                    }
                    return '<div><img alt="" width="50%" height="50%" src="/static/style/img/def_profile.jpg"></div>'
                  }
              }
              ,{field: 'userName', title: '账户'}
              ,{field: 'email', title: '邮箱'}
              ,{field: 'phonenumber', title: '手机号码'}
              ,{field: 'sex', title: "性别",templet: function (d) { return crud.getDictValue('sex',d.sex);}}
              ,{field: 'enabled', align: "center", sort: true, templet: '#enabledTpl', title: '状态'}
              ,{field: 'remark', title: '备注'}
              ,{toolbar: '#tableBar', title: '操作', width: 165, align:'center',fixed: 'right'}
          ]]
      });
      //监听行工具事件
      table.on('tool(userTable)', function(obj){
          let data = obj.data //获得当前行数据
              ,layEvent = obj.event;
          if(layEvent === 'edit'){
              active.editView(data.id);
          } else if(layEvent === 'del'){
              active.del(data.id);
          }
      });

      $('.layui-btn').on('click', function(){
          let type = $(this).data('type');
          active[type] && active[type].call(this);
      });

      /* 触发弹层 */
      let active = {
          /**
           * 添加
           * */
          addView: function(){
              layer.open({
                  type: 2,
                  title: "添加用户",
                  content: ctx + '/sysUser/addPage'
                  ,maxmin: true
                  ,area: ['500px', '600px']
                  ,btn: ['确定', '取消']
                  ,yes: function(index, layero){
                      let submit = layero.find('iframe').contents().find('#save-submit');
                      submit.trigger('click');
                  }
              });
          },
          /**
           * 编辑
           * @param id id
           * */
          editView: function(id){
              layer.open({
                  type: 2
                  ,title: '修改用户'
                  ,content: ctx + '/sysUser/editPage/' + id
                  ,maxmin: true
                  ,area: ['500px', '600px']
                  ,btn: ['确定', '取消']
                  ,yes: function(index, layero){
                      let submit = layero.find('iframe').contents().find('#save-submit');
                      submit.trigger('click');
                  }
              });
          },
          /**
           * 删除
           * @param id id
           */
          del: function(id){
              layer.confirm('真的删除行么', function(index){
                  //向服务端发送删除指令
                  $.ajax({
                      type: 'DELETE',
                      url:  ctx + '/sysUser/del/' + id,
                      contentType:'application/json;charset=UTF-8',
                      dataType: 'json',
                      success: function(result) {
                          layer.msg(result.message);
                          if (result.code === 200) {
                              layer.close(index);
                              table.reload('userTable');
                          }
                      }
                  });
              });
          },
          /**
           * 修改用户状态
           * @param userId 用户id
           * @param checked 状态
           */
          changeUserEnabled: function (userId,checked) {
              $.ajax({
                  type: 'POST',
                  url:  ctx + '/sysUser/changeUser',
                  data: JSON.stringify({
                      id: userId,
                      enabled: checked
                  }),
                  contentType:'application/json;charset=UTF-8',
                  dataType: 'json',
                  success: function(result) {
                      layer.msg(result.message);
                      if (result.code === 200) {
                          table.reload('userTable');
                      }
                  }
              });
          }
      };
  });
  </script>
</body>
</html>
